﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace NConfigGenerator
{
    [DebuggerDisplay("{Name} [{Children.Count} children]")]
    [Serializable]
    public abstract class ConfigNode
    {
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string _name;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly ConfigNode _parent;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly ConfigNodeAttributeCollection _attributes = new ConfigNodeAttributeCollection();

        protected ConfigNode(ConfigNode parent, string name)
        {
            if (name == null)
                throw new ArgumentNullException("name");
            _parent = parent;
            _name = name;
        }

        public string Name
        {
            [DebuggerStepThrough]
            get { return _name; }
            [DebuggerStepThrough]
            set { _name = value; }
        }

        public ConfigNode Parent
        {
            [DebuggerStepThrough]
            get { return _parent; }
        }

        public ConfigNodeAttributeCollection Attributes
        {
            [DebuggerStepThrough]
            get { return _attributes; }
        }

        public abstract IEnumerable<ConfigNode> AllChildren { get; }
    }

    public sealed class ConfigNodeCollection : KeyedCollection<string, ConfigNode>
    {
        protected override string GetKeyForItem(ConfigNode item)
        {
            return item.Name;
        }
    }
}